<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: CategoryGateway.cfc 1181 2007-06-25 21:02:20Z pfarrell $

Notes:
--->
<cfcomponent 
	displayname="CategoryGateway" 
	output="false" 
	extends="CategoryGateway" 
	hint="CategoryGateway for MachBlog (MySQL)">
	
	<cffunction name="init" access="public" output="false" returntype="CategoryGateway" 
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="getCategories" access="public" output="false" returntype="query" 
			hint="Returns a query object containing categories, either all or active only depending on boolean passed in.">
		<cfargument name="includeInactive" type="boolean" required="false" default="false" />
		
		<cfset var getCategories = "" />
		
		<cftry>
			<cfquery name="getCategories" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT 	machblog_category.category_id, 
						machblog_category.category_name, 
						machblog_category.dt_created, 
						COUNT(machblog_entry_category.entry_id) AS num_posts  
				FROM 	(machblog_category 
							LEFT OUTER JOIN machblog_entry_category 
								ON machblog_category.category_id = machblog_entry_category.category_id) 
				LEFT OUTER JOIN machblog_entry 
				ON machblog_entry_category.entry_id = machblog_entry.entry_id 
				AND machblog_entry.is_active = <cfqueryparam value="1" cfsqltype="cf_sql_tinyint" /> 
				<cfif Not arguments.includeInactive>
				WHERE machblog_category.is_active = <cfqueryparam value="1"	cfsqltype="cf_sql_tinyint" />
				<!---
				AND machblog_entry.dt_posted <= <cfqueryparam value="#getResourceBundleFacade().getLocaleUtils().toEpoch(Now())#" 
																	cfsqltype="cf_sql_bigint" />
				--->
				</cfif>
				GROUP BY 	machblog_category.category_id, machblog_category.category_name, 
							machblog_category.dt_created 
				ORDER BY category_name ASC
			</cfquery>
			<cfcatch type="database">
				<cfrethrow />
			</cfcatch>
		</cftry>
		
		<cfreturn getCategories />
	</cffunction>
</cfcomponent>
